package com.inossem.warehousestorage_inossem.activity.up_frame;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.extra.UpFrameExtra;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameCommitRespBean;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameOverviewBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.up_frame.UpFrameCommitRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.inossem.warehousestorage_inossem.utils.UpFrameUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

public class UpFrameCommitActivity extends BaseActivity {

    private TextView title, requestNumber, operateTime, needUp, doUp, operator, confirm;

    private PercentRelativeLayout submitOverviewLayout;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_up_frame_commit);
        initView(UpFrameCommitActivity.this);
        initData(this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        requestNumber = (TextView) activity.findViewById(R.id.requestNumber);
        operateTime = (TextView) activity.findViewById(R.id.operateTime);
        needUp = (TextView) activity.findViewById(R.id.needUp);
        doUp = (TextView) activity.findViewById(R.id.doUp);
        confirm = (TextView) activity.findViewById(R.id.confirm);
        operator = (TextView) activity.findViewById(R.id.operator);
        submitOverviewLayout = (PercentRelativeLayout) activity.findViewById(R.id.secondBlock);
        confirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                UpFrameConfirmRequestBean params = UpFrameUtils.getUpFrameConfirmRequestBean(activity);
                UpFrameCommitRequest.request(activity, params, new GsonBeanListener<UpFrameCommitRespBean>() {
                    @Override
                    public void onSuccess(UpFrameCommitRespBean bean) {
                        if (bean.getData().getCode().equals(RequestConstant.BEAN_CODE_SUCCESS)) {
                            SPUtils.removeUpFrameInformation(activity);
                            Intent intent = new Intent(activity, UpFrameResultActivity.class);
                            intent.putExtra(UpFrameExtra.MESSAGE, bean.getData().getMsg());
                            intent.putExtra(UpFrameExtra.CONFIRM_MESSAGE, "请求作业号" + params.getRequestNumber() + activity.getResources().getString(R.string.activity_receive_result_has_confirmed));
                            startActivity(intent);
                        } else {
                            if (!TextUtils.isEmpty(bean.getData().getMsg())) {
                                Utils.middleToast(activity, bean.getData().getMsg());
                            }
                        }
                    }
                });
            }
        });
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_up_frame_commit_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_up_frame_commit_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getData(activity);
    }


    private void getData(Context context) {
        UpFrameOverviewBean bean = SPUtils.getUpFrameInformation(context);
        requestNumber.setText(bean.getData().getRequestNumber());
        operator.setText(SPUtils.getName(context));
        operateTime.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
        needUp.setText(bean.getData().getMaterielList().size() + "");
        doUp.setText(UpFrameUtils.getUpFrameConfirmNumber(context) + "");
        submitOverviewLayout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getOverviewDialog(context, UpFrameUtils.getOverViewBeanWithUpFrameList(bean.getData().getMaterielList())).show();
            }
        });

    }

}
